package com.attendance.manage.controller;

import com.attendance.common.annotation.Log;
import com.attendance.common.core.controller.BaseController;
import com.attendance.common.core.domain.AjaxResult;
import com.attendance.common.core.page.TableDataInfo;
import com.attendance.common.enums.BusinessType;
import com.attendance.common.utils.poi.ExcelUtil;
import com.attendance.manage.domain.Attendance;
import com.attendance.manage.domain.AttendanceStatDTO;
import com.attendance.manage.service.IAttendanceService;
import com.attendance.manage.service.ICommonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 考勤记录Controller
 *
 * @author ruoyi
 * @date 2025-11-01
 */
@RestController
@RequestMapping("/attendance-manage/common")
public class CommonsController extends BaseController
{
    @Autowired
    private IAttendanceService attendanceService;
    @Autowired
    private ICommonsService commonsService;

    /**
     * 查询考勤记录列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Attendance attendance)
    {
        startPage();
        String username = getLoginUser().getUser().getNickName();
        attendance.setUsername(username);
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        return getDataTable(list);
    }
    //查询当月的全部考勤记录
    @GetMapping("/month")
    public AjaxResult month()
    {
//        String username = getLoginUser().getUser().getNickName();
//        Attendance attendance = new Attendance();
//        attendance.setUsername(username);
//        //将ClockInTime设置为当前月份第一天
//        Date startDate = Date.from(LocalDate.now().withDayOfMonth(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
//        attendance.setClockInTime(startDate);
//        System.out.println("startDate:" + startDate);
//        //将ClockInTime设置为今天23 点
//        Date endDate = Date.from(LocalDate.now().atTime(23, 59, 59).atZone(ZoneId.systemDefault()).toInstant());
//        attendance.setClockOutTime(endDate);
//        System.out.println("endDate:" + endDate);
//        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
//        AttendanceStatDTO attendanceStatDTO = new AttendanceStatDTO();
//        attendanceStatDTO.setLateCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("迟到")).count());
//        attendanceStatDTO.setEarlyLeaveCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("早退")).count());
//        attendanceStatDTO.setAbsentCount((int) list.stream().filter(attendance1 -> attendance1.getStatus().equals("旷工")).count());
//        System.out.println(attendanceStatDTO);
//        return success(attendanceStatDTO);
        return commonsService.selectmonth();
    }

    /**
     * 导出考勤记录列表
     */

    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attendance attendance)
    {
        List<Attendance> list = attendanceService.selectAttendanceList(attendance);
        ExcelUtil<Attendance> util = new ExcelUtil<Attendance>(Attendance.class);
        util.exportExcel(response, list, "考勤记录数据");
    }

    /**
     * 获取考勤记录详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attendanceService.selectAttendanceById(id));
    }

    /**
     * 新增考勤记录
     */


    @PostMapping("/add")
    public AjaxResult add()
    {
        Attendance attendance = new Attendance();
        attendance.setUsername(getLoginUser().getUser().getNickName());
        attendance.setUserId(getLoginUser().getUser().getUserId());
        System.out.println( attendance);
        int i = attendanceService.insertAttendance(attendance);
        if (i == 10000){
            return error("今天已经打卡了");
        }
        return toAjax(i);
    }

    /**
     * 修改考勤记录
     */

    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit()
    {
        Attendance attendance = new Attendance();
        attendance.setUsername(getLoginUser().getUser().getNickName());
        attendance.setUserId(getLoginUser().getUser().getUserId());
        System.out.println( attendance);
        int i = attendanceService.updateAttendance(attendance);
        if (i == 10000){
            return error("还没有签到");
        }
        if (i == 10001){
            return error("已经签退了");
        }
        return toAjax(i);
    }

    /**
     * 删除考勤记录
     */

    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceService.deleteAttendanceByIds(ids));
    }
}

